package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.wordmemorize.dao.entity.ReviewSubTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.ReviewTopicDO;
import com.cskaoyan.wordmemorize.dao.entity.TopicOptionDO;
import com.cskaoyan.wordmemorize.dao.entity.VocDO;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewSubTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.ReviewTopicMapper;
import com.cskaoyan.wordmemorize.dao.mapper.TopicOptionsMapper;
import com.cskaoyan.wordmemorize.dao.mapper.VocMapper;
import com.cskaoyan.wordmemorize.excel.FillExcelData;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class FillExcelDataListener implements ReadListener<FillExcelData> {

    @Getter
    public  int lines;

    VocMapper vocMapper;
    ReviewTopicMapper reviewTopicMapper;
    ReviewSubTopicMapper reviewSubTopicMapper;
    TopicOptionsMapper topicOptionsMapper;
    public FillExcelDataListener(VocMapper vocMapper, ReviewTopicMapper reviewTopicMapper, ReviewSubTopicMapper reviewSubTopicMapper, TopicOptionsMapper topicOptionsMapper) {
        this.vocMapper = vocMapper;
        this.reviewTopicMapper = reviewTopicMapper;
        this.reviewSubTopicMapper = reviewSubTopicMapper;
        this.topicOptionsMapper = topicOptionsMapper;
    }

    @Override
    public void invoke(FillExcelData fillExcelData, AnalysisContext analysisContext) {
        List<String> topics = new ArrayList<>();
        List<String> answers = new ArrayList<>();
        String subTopicOne = fillExcelData.getSubTopicOne();
        topics.add(subTopicOne);
        String subTopicTwo = fillExcelData.getSubTopicTwo();
        topics.add(subTopicTwo);
        String subTopicThree = fillExcelData.getSubTopicThree();
        topics.add(subTopicThree);
        StringBuilder TopicSBuilder = new StringBuilder();
        TopicSBuilder.append(subTopicOne).append("\n").append(subTopicTwo).append("\n").append(subTopicThree);
        String answerOne = fillExcelData.getAnswerOne();
        answers.add(answerOne);
        String answerTwo = fillExcelData.getAnswerTwo();
        answers.add(answerTwo);
        String answerThree = fillExcelData.getAnswerThree();
        answers.add(answerThree);
        StringBuilder vocSBuilder = new StringBuilder();
        vocSBuilder.append(answerOne).append(",").append(answerTwo).append(",").append(answerThree);
        String disturbAnswer = fillExcelData.getDisturbAnswer();
        //插入review_topic表
        ReviewTopicDO reviewTopicDO = new ReviewTopicDO();
        reviewTopicDO.setTopic(TopicSBuilder.toString());
        reviewTopicDO.setVocabulary(vocSBuilder.toString());
        reviewTopicDO.setTopicType(2);
        reviewTopicMapper.insert(reviewTopicDO);
        Long tid = reviewTopicDO.getId();
        //插入review_sub_topic表
        for(int i = 0; i < topics.size(); i++) {
            String topic = topics.get(i);
            String answer = answers.get(i);
            QueryWrapper<VocDO> vocMapperQueryWrapper = new QueryWrapper<>();
            vocMapperQueryWrapper.eq("content", answer);
            VocDO vocDO = vocMapper.selectOne(vocMapperQueryWrapper);
            Long vid = vocDO.getId();
            ReviewSubTopicDO reviewSubTopicDO = new ReviewSubTopicDO();
            reviewSubTopicDO.setTid(tid);
            reviewSubTopicDO.setVocId(vid);
            reviewSubTopicDO.setTopicItem(topic);
            reviewSubTopicDO.setVocabulary(answer);
            reviewSubTopicDO.setSubTopicNumber(i+1);
            reviewSubTopicMapper.insert(reviewSubTopicDO);
        }
        TopicOptionDO topicOptionDO = new TopicOptionDO();
        topicOptionDO.setTid(tid);
        topicOptionDO.setItem(disturbAnswer);
        topicOptionDO.setIsAnswer(0);
        topicOptionsMapper.insert(topicOptionDO);
        lines++;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
